package sharemem

import (
	"encoding/json"
	"server_cluster/common/config"
	"server_cluster/common/gdconf"
	"server_cluster/common/logger"
	"server_cluster/common/protoc/pb"
	"server_cluster/common/tools"
	"time"
)

const (
	RefreshShopHourOffset = 0
)

type ShopGoods struct {
	GoodsId  int32
	BuyCount int32
}

func (s *ShopGoods) IsUse() bool {
	return s.GoodsId != 0
}

func (s *ShopGoods) MemInit() {
	s.GoodsId = 0
	s.BuyCount = 0
}

type UserShop struct {
	BuyGoodsList        [500]ShopGoods
	LastShopRefreshTime int64
}

func (s UserShop) MarshalJSON() ([]byte, error) {
	dbUserShop := struct {
		BuyGoodsList        []ShopGoods
		LastShopRefreshTime int64
	}{}
	for _, shopGoods := range s.BuyGoodsList {
		if !shopGoods.IsUse() {
			continue
		}
		dbUserShop.BuyGoodsList = append(dbUserShop.BuyGoodsList, shopGoods)
	}
	dbUserShop.LastShopRefreshTime = s.LastShopRefreshTime
	data, err := json.Marshal(dbUserShop)
	if err != nil {
		return nil, err
	}
	return data, nil
}

func (s *UserShop) UnmarshalJSON(data []byte) error {
	dbUserShop := struct {
		BuyGoodsList        []ShopGoods
		LastShopRefreshTime int64
	}{}
	err := json.Unmarshal(data, &dbUserShop)
	if err != nil {
		return err
	}
	for index, shopGoods := range dbUserShop.BuyGoodsList {
		s.BuyGoodsList[index] = shopGoods
	}
	s.LastShopRefreshTime = dbUserShop.LastShopRefreshTime
	return nil
}

func (s *UserShop) MemInit() {
	for i := 0; i < len(s.BuyGoodsList); i++ {
		s.BuyGoodsList[i].MemInit()
	}
	s.LastShopRefreshTime = 0
}

func (u *User) RefreshShop() int16 {
	nowOffset := tools.ParseTimeToObj(MyShm.TimeNow).Add(-(time.Hour * RefreshShopHourOffset))
	lastOffset := time.Unix(u.UserShop.LastShopRefreshTime, 0).Add(-(time.Hour * RefreshShopHourOffset))
	if nowOffset.Year() == lastOffset.Year() && nowOffset.YearDay() <= lastOffset.YearDay() {
		return config.OK
	}
	u.UserShop.LastShopRefreshTime = MyShm.TimeNow
	logger.Debug("[RefreshShop] start, uid: %v", u.GetUid())
	// 每日刷新
	for i := 0; i < len(u.UserShop.BuyGoodsList); i++ {
		shopGoods := &(u.UserShop.BuyGoodsList[i])
		if shopGoods.GoodsId == 0 {
			continue
		}
		shopConfig := gdconf.GetShopConfig(shopGoods.GoodsId)
		if shopConfig == nil {
			continue
		}
		if shopConfig.BuyLimitType != gdconf.ShopBuyLimitTypeDay {
			continue
		}
		u.DeleteBuyShopRecord(shopGoods)
		shopGoods.MemInit()
	}
	// 每周刷新
	if nowOffset.Weekday() == time.Monday {
		for i := 0; i < len(u.UserShop.BuyGoodsList); i++ {
			shopGoods := &(u.UserShop.BuyGoodsList[i])
			if shopGoods.GoodsId == 0 {
				continue
			}
			shopConfig := gdconf.GetShopConfig(shopGoods.GoodsId)
			if shopConfig == nil {
				continue
			}
			if shopConfig.BuyLimitType != gdconf.ShopBuyLimitTypeWeek {
				continue
			}
			u.DeleteBuyShopRecord(shopGoods)
			shopGoods.MemInit()
		}
	}
	// 每月刷新
	if nowOffset.Day() == 1 {
		for i := 0; i < len(u.UserShop.BuyGoodsList); i++ {
			shopGoods := &(u.UserShop.BuyGoodsList[i])
			if shopGoods.GoodsId == 0 {
				continue
			}
			shopConfig := gdconf.GetShopConfig(shopGoods.GoodsId)
			if shopConfig == nil {
				continue
			}
			if shopConfig.BuyLimitType != gdconf.ShopBuyLimitTypeMonth {
				continue
			}
			u.DeleteBuyShopRecord(shopGoods)
			shopGoods.MemInit()
		}
	}
	logger.Debug("[RefreshShop] finish, uid: %v", u.GetUid())
	return config.OK
}

func PacketShopGoods(goodsId int32, user *User) *pb.ShopGoods {
	shopConfig := gdconf.GetShopConfig(goodsId)
	if shopConfig == nil {
		return nil
	}
	buyCount := user.GetBuyShopRecord(shopConfig.ShopGoodsId)
	ret := &pb.ShopGoods{
		GoodsId:     shopConfig.ShopGoodsId,
		RemainCount: shopConfig.BuyLimitCount - buyCount,
	}
	if shopConfig.StartTime != nil {
		ret.StartTime = int32(shopConfig.StartTime.Unix())
	}
	if shopConfig.EndTime != nil {
		ret.EndTime = int32(shopConfig.EndTime.Unix())
	}
	return ret
}
func (u *User) AddShopRecord(shopId, count int32) {
	for i := 0; i < len(u.UserShop.BuyGoodsList); i++ {
		var shopRecord = &(u.UserShop.BuyGoodsList[i])
		if shopRecord.IsUse() {
			if shopRecord.GoodsId == shopId {
				shopRecord.BuyCount += count
				break
			}
			continue
		}
		shopRecord.GoodsId = shopId
		shopRecord.BuyCount = count
		u.AddBuyShopRecord(shopId, shopRecord)
		break
	}
}
